package com.augmentum.login.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.augmentum.login.Constants;
import com.augmentum.login.dao.QuestionDao;
import com.augmentum.login.exception.ParamException;
import com.augmentum.login.exception.ServiceException;
import com.augmentum.login.model.Page;
import com.augmentum.login.model.Question;
import com.augmentum.login.service.QuestionService;
import com.augmentum.login.util.Regular;
import com.augmentum.login.util.StringUtil;

@Service("questionService")
public class QuestionServiceImpl implements
        QuestionService {

    @Resource
    private QuestionDao questionDao;

    @Override
    public List<Question> getQuestionList() {
        return questionDao.findQuestionList();
    }

    @Override
    public void removeQuestion(String questionId) {
        questionDao.deleteQuestion(questionId);
    }

    @Override
    public void addQuestion(Question question) throws ParamException,
            ServiceException {
        ParamException paramException = new ParamException();
        if (StringUtil.isEmpty(question.getQuestionId())) {
            paramException.addExceptionField(Constants.QUESTION_ID_EXCEPTION,
                    Constants.VALUE_EMPTY);
        } else if (!question.getQuestionId().matches(
                Regular.QUESTIONID.getValue())) {
            paramException.addExceptionField(Constants.QUESTION_ID_EXCEPTION,
                    Constants.WRONG);
        }

        if (StringUtil.isEmpty(question.getDescription())) {
            paramException.addExceptionField(Constants.DESCRIPTION_EXCEPTION,
                    Constants.VALUE_EMPTY);
        } else if (!question.getDescription().matches(
                Regular.NONEEMPTY400.getValue())) {
            paramException.addExceptionField(Constants.DESCRIPTION_EXCEPTION,
                    Constants.WRONG);
        }

        if (StringUtil.isEmpty(question.getOptionA())) {
            paramException.addExceptionField(Constants.OPTION_A_EXCEPTION,
                    Constants.VALUE_EMPTY);
        } else if (!question.getOptionA().matches(
                Regular.NONEEMPTY200.getValue())) {
            paramException.addExceptionField(Constants.OPTION_A_EXCEPTION,
                    Constants.WRONG);
        }

        if (StringUtil.isEmpty(question.getOptionB())) {
            paramException.addExceptionField(Constants.OPTION_B_EXCEPTION,
                    Constants.VALUE_EMPTY);
        } else if (!question.getOptionB().matches(
                Regular.NONEEMPTY200.getValue())) {
            paramException.addExceptionField(Constants.OPTION_B_EXCEPTION,
                    Constants.WRONG);
        }

        if (StringUtil.isEmpty(question.getOptionC())) {
            paramException.addExceptionField(Constants.OPTION_C_EXCEPTION,
                    Constants.VALUE_EMPTY);
        } else if (!question.getOptionC().matches(
                Regular.NONEEMPTY200.getValue())) {
            paramException.addExceptionField(Constants.OPTION_C_EXCEPTION,
                    Constants.WRONG);
        }

        if (StringUtil.isEmpty(question.getOptionD())) {
            paramException.addExceptionField(Constants.OPTION_D_EXCEPTION,
                    Constants.VALUE_EMPTY);
        } else if (!question.getOptionD().matches(
                Regular.NONEEMPTY200.getValue())) {
            paramException.addExceptionField(Constants.OPTION_D_EXCEPTION,
                    Constants.WRONG);
        }

        if (StringUtil.isEmpty(question.getAnswer())) {
            paramException.addExceptionField(Constants.ANSWER_EXCEPTION,
                    Constants.VALUE_EMPTY);
        } else if (!question.getAnswer().matches(
                Regular.NONEEMPTY200.getValue())) {
            paramException.addExceptionField(Constants.ANSWER_EXCEPTION,
                    Constants.WRONG);
        }

        if (paramException.hasExceptionFiled()) {
            throw paramException;
        }

        if (questionDao.findQuestionById(question.getQuestionId()).size() > 0) {
            throw new ServiceException(1, Constants.NONE_EXIST);
        }
        questionDao.createQuestion(question);
    }

    @Override
    public void modifyQuestion(Question question) throws ParamException {

        ParamException paramException = new ParamException();
        if (StringUtil.isEmpty(question.getDescription())) {
            paramException.addExceptionField(Constants.DESCRIPTION_EXCEPTION,
                    Constants.VALUE_EMPTY);
        } else if (!question.getDescription().matches(
                Regular.NONEEMPTY400.getValue())) {
            paramException.addExceptionField(Constants.DESCRIPTION_EXCEPTION,
                    Constants.WRONG);
        }

        if (StringUtil.isEmpty(question.getOptionA())) {
            paramException.addExceptionField(Constants.OPTION_A_EXCEPTION,
                    Constants.VALUE_EMPTY);
        } else if (!question.getOptionA().matches(
                Regular.NONEEMPTY200.getValue())) {
            paramException.addExceptionField(Constants.OPTION_A_EXCEPTION,
                    Constants.WRONG);
        }

        if (StringUtil.isEmpty(question.getOptionB())) {
            paramException.addExceptionField(Constants.OPTION_B_EXCEPTION,
                    Constants.VALUE_EMPTY);
        } else if (!question.getOptionB().matches(
                Regular.NONEEMPTY200.getValue())) {
            paramException.addExceptionField(Constants.OPTION_B_EXCEPTION,
                    Constants.WRONG);
        }

        if (StringUtil.isEmpty(question.getOptionC())) {
            paramException.addExceptionField(Constants.OPTION_C_EXCEPTION,
                    Constants.VALUE_EMPTY);
        } else if (!question.getOptionC().matches(
                Regular.NONEEMPTY200.getValue())) {
            paramException.addExceptionField(Constants.OPTION_C_EXCEPTION,
                    Constants.WRONG);
        }

        if (StringUtil.isEmpty(question.getOptionD())) {
            paramException.addExceptionField(Constants.OPTION_D_EXCEPTION,
                    Constants.VALUE_EMPTY);
        } else if (!question.getOptionD().matches(
                Regular.NONEEMPTY200.getValue())) {
            paramException.addExceptionField(Constants.OPTION_D_EXCEPTION,
                    Constants.WRONG);
        }

        if (StringUtil.isEmpty(question.getAnswer())) {
            paramException.addExceptionField(Constants.ANSWER_EXCEPTION,
                    Constants.VALUE_EMPTY);
        } else if (!question.getAnswer().matches(
                Regular.NONEEMPTY200.getValue())) {
            paramException.addExceptionField(Constants.ANSWER_EXCEPTION,
                    Constants.WRONG);
        }

        if (paramException.hasExceptionFiled()) {
            throw paramException;
        }
        questionDao.updateQuestion(question);
    }

    @Override
    public Question getQuestion(String questionId) {
        return questionDao.getQuestionById(questionId);
    }

    @Override
    public Question getQuestion(int id) {
        return questionDao.getById(id);
    }

    @Override
    public void removeBathcQuestions(String questionStr, String type) {
        questionDao.updateState(questionStr.replaceAll(" ", "").split(type));
    }

    @Override
    public List<Question> searchQuestionsByPage(String description, String order, Page page) {
        return questionDao.findQuestionsByPage(description, order, page);
    }

    @Override
    public int getQuestionCount(String value) {
        return questionDao.getQuestionCount(value);
    }
}
